<template>
  <div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      @row-update="rowUpdate"
      @row-save="rowSave"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="primary" size="small" plain icon="el-icon-plus" @click="handleSectionRoute">新 增</el-button>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button type="text" size="small" icon="el-icon-view" plain @click="handleView(row)" style="border: 0 !important">详情 </el-button>
        <el-button type="text" size="small" icon="el-icon-edit" plain @click="handleEdit(row)" style="border: 0 !important">编辑 </el-button>
        <el-button type="text" size="small" icon="el-icon-delete" plain @click="handleDelete(row)" style="border: 0 !important">删除 </el-button>
      </template>
      <template #isTraceForm>
        <el-switch v-model="form.isTrace" active-text="开启" inactive-text="关闭" :disabled="option.addTitle === '详情'"></el-switch>
      </template>
      <template #exceptionDurationForm>
        <div>
          <span style="margin-right: 5px">≥</span>
          <el-input-number v-model="form.exceptionDuration" controls-position="right" :min="0" name="exceptionDuration"></el-input-number>
          <span style="margin-left: 5px">分钟(min)</span>
        </div>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { getList, add, update, getDetail, remove } from '@/api/product/productSection';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
export default {
  components: {
    userTableConfig
  },
  data() {
    return {
      pageCode: 'ProductSectionDefinition',
      visible: false,
      form: {},
      data: [],
      defaults: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 420px)',
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        searchLabelWidth: 100,
        labelWidth: 110,
        dialogCustomClass: 'custom-class',
        border: true,
        index: true,
        indexLabel: '序号',
        menuWidth: 200,
        addBtn: false,
        viewBtn: false,
        viewBtnText: '详情',
        viewTitle: '详情',
        editBtn: false,
        delBtn: false,
        refreshBtn: true,
        excelBtn: false,
        selection: true,
        dialogDrag: false, // 开启拖拽功能
        dialogClickModal: false,
        columnBtn: false,
        column: [
          {
            label: '产品工段编码',
            prop: 'code',
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请输入产品工段编码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '产品工段名称',
            prop: 'name',
            search: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请输入产品工段名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '关联生产工段',
            prop: 'productWorkSectionName',
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false
          },
          {
            label: '颜色标识',
            prop: 'color',
            type: 'color', //增加颜色选择器
            colorFormat: 'hex',
            // showAlpha: false,
            width: 120,
            slot: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            rules: [
              {
                required: true,
                message: '请选择颜色标识',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '备注',
            prop: 'remark',
            slot: true,
            overHidden: true,
            type: 'textarea',
            minRows: 3,
            maxRows: 3,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false
          },
          {
            label: '创建时间',
            prop: 'createTime',
            width: 150,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false
          },
          {
            label: '工段追溯',
            prop: 'isTrace',
            type: 'switch',
            hide: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            dicData: [
              { label: '关闭', value: false },
              { label: '开启', value: true }
            ],
            value: false,
            labelTip: '开启后，正向追溯可显示该工段的用料信息'
          }
        ],
        group: [
          {
            label: '工段信息',
            prop: 'sectionInfo',
            arrow: false,
            icon: 'el-icon-vertical-bar',
            // addDisplay: false,
            // editDisplay: false,
            column: [
              {
                label: '产品工段编码',
                prop: 'code',
                rules: [
                  {
                    required: true,
                    message: '请输入产品工段编码',
                    trigger: 'blur'
                  }
                ]
              },
              {
                label: '产品工段名称',
                prop: 'name',
                rules: [
                  {
                    required: true,
                    message: '请输入产品工段名称',
                    trigger: 'blur'
                  }
                ]
              },
              {
                label: '关联生产工段',
                prop: 'productWorkSectionId',
                type: 'select',
                dicUrl: '/api/mes-product/product/worksection/all-section',
                props: {
                  label: 'name',
                  value: 'id'
                },
                rules: [
                  {
                    required: true,
                    message: '请选择关联生产工段',
                    trigger: 'change'
                  }
                ]
              },
              {
                label: '颜色标识',
                prop: 'color',
                type: 'color', //增加颜色选择器
                colorFormat: 'hex',
                // showAlpha: false,
                readonly: true,
                slot: true,
                rules: [
                  {
                    required: true,
                    message: '请选择颜色标识',
                    trigger: 'blur'
                  }
                ]
              },
              {
                label: '工段追溯',
                prop: 'isTrace',
                type: 'switch',
                dicData: [
                  { label: '关闭', value: false },
                  { label: '开启', value: true }
                ],
                value: true,
                labelTip: '开启后，正向追溯可显示该工段的用料信息'
              },
              {
                label: '备注',
                prop: 'remark',
                cell: true,
                slot: true,
                type: 'textarea',
                minRows: 3,
                maxRows: 3,
                span: 24
              }
            ]
          },
          {
            label: '停线识别设置',
            prop: 'timeSetting',
            arrow: false,
            icon: 'el-icon-vertical-bar',
            column: [
              {
                label: '异常间隔时间判定',
                labelWidth: 150,
                span: 24,
                prop: 'exceptionDuration',
                type: 'number',
                slot: true,
                rules: [
                  {
                    required: false,
                    message: '请输入数值',
                    trigger: 'blur'
                  }
                ]
              }
            ]
          }
        ]
      }
    };
  },
  mounted() {},
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        // addBtn: this.vaildData(this.permission.pallet_add, false),
        // viewBtn: this.vaildData(this.permission.pallet_view, false),
        // delBtn: this.vaildData(this.permission.pallet_delete, false),
        // editBtn: this.vaildData(this.permission.pallet_edit, false)
      };
    }
  },
  methods: {
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          let data = res.data.data;
          this.form = data || {};
        });
      }
      done();
    },
    rowSave(row, done, loading) {
      let params = {
        code: row.code,
        name: row.name,
        color: row.color,
        isTrace: row.isTrace,
        exceptionDuration: row.exceptionDuration,
        remark: row.remark,
        productWorkSectionId: row.productWorkSectionId
      };
      console.log(params);
      add(params).then(
        (res) => {
          let data = res.data.data;
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.onLoad(this.page);
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        (res) => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          if (row.child) {
            data.child = true;
          }
          this.onLoad(this.page);
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    handleEdit(row) {
      this.$refs.crud.rowEdit(row);
    },
    handleView(row) {
      this.$refs.crud.rowView(row);
    },
    //新增
    handleSectionRoute() {
      this.$refs.crud.rowAdd();
    },
    beforeClose(done) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },

    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.selectionClear();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleDelete(row) {
      this.$confirm(`是否确认将${row.name}工段删除?`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return remove(row.id)
          .then((result) => {
            this.$message.success(result.data.msg);
            this.onLoad(this.page);
          })
          .catch((err) => {});
      });
    }
  }
};
</script>

<style lang="scss">
.btn {
  padding: 9px 15px;
  font-size: 10px;
  height: 35px;
  background-color: #409eff !important;
  color: #cccccc !important;
}

.is-disabled {
  background-color: #fff !important;
  color: #cccccc !important;
}

.title {
  color: #000;
}

.title:hover {
  color: #000;
}

.el-transfer-button {
  display: flex;
  flex-direction: row;
  height: 100%;
  margin: 40px 12px 0;
  align-items: center;
}

.search-container {
  display: flex;
  justify-content: space-between;
}

.search-item {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.el-color-picker__trigger {
  height: 35px;
  width: 35px;
}
</style>
